#include  <stdint.h>
#include  "mid_lcd.h"
#include  "mid_key.h"
#include  "mid_beep.h"
#include  "mid_bus.h"
#include  "mid_timer.h"
#include	"driver.h"
#include  "app.h"

static cmdPROBUF tmp;
static void disp_ui_v(uint8 dis1, uint8 dis2, uint8 dis3)
{		
		if(UIoperate.CodeFlag)
			mid_lcdbianma(DISPON);
		else
			mid_lcdbianma(DISPOFF);
		
		if(UIoperate.SetFlag)
			mid_lcdshezhi(DISPON);
		else
			mid_lcdshezhi(DISPOFF);		
		
		if(UIoperate.Chongdian)
			mid_lcdchongdian(DISPON);
		else
			mid_lcdchongdian(DISPOFF);		
		
		if(UIoperate.Wrong)
			mid_lcdcuowu(DISPON);
		else
			mid_lcdcuowu(DISPOFF);		

	  if(UIoperate.LockFlag)
			mid_lcdsuoding(DISPON);
		else
			mid_lcdsuoding(DISPOFF);			
		
	  if(UIoperate.ContinuesFlag)
			mid_lcdlianxu(DISPON);
		else
			mid_lcdlianxu(DISPOFF);

	  if(UIoperate.Correct)
			mid_lcdzhengque(DISPON);
		else
			mid_lcdzhengque(DISPOFF);		
		
//		if(UIoperate.Addr)
//			mid_lcddizhi(DISPON);
//		else
//			mid_lcddizhi(DISPOFF);
		
		if(UIoperate.Data)
			mid_lcdshuju(DISPON);
		else
			mid_lcdshuju(DISPOFF);
		
		if(UIoperate.RdFlag)
			mid_lcdduchu(DISPON);
		else
			mid_lcdduchu(DISPOFF);
		
		if(UIoperate.WrFlag)
			mid_lcdxieru(DISPON);
		else
			mid_lcdxieru(DISPOFF);
		
		UIpos[0] = 0;//F
		UIpos[1] = UIoperate.NowSet/10;//
		UIpos[2] = UIoperate.NowSet%10;//		
	  mid_lcddisp_line1(UIpos[0], UIpos[1], UIpos[2]);
	  
		mid_lcddisp_line2(dis1, dis2, dis3);
		mid_lcdp4(DISPON);
		//mid_lcdp1(DISPON);
	  strLcdD.frush = 1;

}
			 
			 
void UI_YT_SJ(void)
{
			
			BUS_WORK();				
      if(GetKey(KEY_PRESS_UP))   
			{
						  
							
							UIoperate.Addr = 0;
							UIoperate.Data = 0;
							UIoperate.Wrong = 0;
							UIoperate.Correct = 0;
							UIoperate.RdFlag = 0;
							UIoperate.WrFlag = 0;
							switch (valuekey.val)
							 {
									case 0x21:										//READ     
												
												UIoperate.ContinuesFlag = 1;
												tmp.ch = 0;
												tmp.cmd = 0x25;
												tmp.addr = 0;
												write_cmfbuf(&tmp);												
												disp_ui_v(ReturnData/100, ReturnData/10%10, ReturnData%10);
												break;
											

												
												

												
													
										
									
												
									
									case 0x23:     								//0
									case 0x31:     								//7
									case 0x33:   									//8
									case 0x35:    								//9
									case 0x41:      							//4
									case 0x43:      							//5
									case 0x45:      							//6
									case 0x53:      							//2
									case 0x55:      							//3
									case 0x51:      							//1										
												break;
									
									case 0x25:     								//WRITE
												break;
									
									case 0x27:     
												break;
									
									case 0x37:      							//LIGHT																						 
											  if(UIoperate.Backlight == 0)
											  {
												  UIoperate.Backlight = 1;
												  LCDBLACKON();
											  }
											  else
											  {
												  UIoperate.Backlight = 0;
												  LCDBLACKOFF();											 
											  }
												break;

									case 0x47: 										//TEST																	
												break;

									case 0x57: 					 					//SET														 
												
												break;
									
									case 0x11: 					 					//ESC											
												UIoperate.ContinuesFlag = 0;
												break;
									
									case 0x13: 					 					//CONTINUE
												break;
									
									case 0x15: 					 					//OK
												break;
									
									case 0x17: 					 					//CODE
												UINUM = 2;
												break;				 
							 }			 
				
	}
			
		if(strLcdD.UIfresh)
		{			
			if(strLcdD.UIinit)
			{
				strLcdD.UIinit = 0;
				UIoperate.LastOperate = 0;
				UIoperate.ContinuesFlag = 0;
				UIoperate.Chongdian = 1;
				UIoperate.Correct = 0;
				UIoperate.Addr = 0;
				UIoperate.Data = 0;
				UIoperate.SetFlag = 1;
				UIoperate.CodeFlag = 0;
				UIoperate.LockFlag = 1;			
				UIoperate.Wrong = 0;
				UIoperate.RdFlag = 0;
				UIoperate.WrFlag = 0;
				UIoperate.NowSet = 1;
				UIoperate.IR_Start = 0;
				UIposition = 0;
				ReturnData = 0;							
			}			
			disp_ui_v(ReturnData/100, ReturnData/10%10, ReturnData%10);
			strLcdD.UIfresh = 0;		
		}
		
		if(strLcdD.TimeCall)//100ms
		{
			strLcdD.TimeCall = 0;
      if(UIoperate.TimeCallDelay){UIoperate.TimeCallDelay--;return;}
			UIoperate.TimeCallDelay = 15;
			if(UIoperate.Correct)
			{				
				//BEEP_ON();
				UIoperate.Wrong = 0;
				UIoperate.WaitData = 0;
				disp_ui_v(ReturnData/100, ReturnData/10%10, ReturnData%10);
				UIoperate.Correct = 0;//after display
			}
			
			if(UIoperate.ContinuesFlag)
			{
//						tmp.ch = 0;
//						tmp.cmd = 0x7a;
//						tmp.addr = 0;
//						tmp.dat = 0x0b;
//						write_cmfbuf(&tmp);	
					if(UIoperate.TimeCallDelay1){UIoperate.TimeCallDelay1--;}
					else
					{
						UIoperate.TimeCallDelay1 = 1;//2s
						UIoperate.Wrong = 0;
						tmp.ch = 0;
						tmp.cmd = 0x25;
						tmp.addr = 0;
						write_cmfbuf(&tmp);					
						UIoperate.WaitData = 1;
					}
			}
	
			if(UIoperate.WaitData)
			{
				UIoperate.WaitDataTime++;
				if(UIoperate.WaitDataTime>7)//5s  timeout add send
				{
					UIoperate.WaitDataTime = 0;
					UIoperate.Wrong = 1;
					UIoperate.WaitData = 0;
					disp_ui_v(ReturnData/100, ReturnData/10%10, ReturnData%10);
					tmp.ch = 0;
				  tmp.cmd = 0x25;
				  tmp.addr = 0;
				  write_cmfbuf(&tmp);				
				}
			}
				
			if(ReturnData >= 13 && ReturnData < 25)
				{
					tmp.ch = 0;
					tmp.cmd = 0x7B;
					tmp.addr = 0;
					tmp.dat = 0x06;
					write_cmfbuf(&tmp);										
				}
//				if(ReturnData > 50 && ReturnData < 60)
//				{
//					tmp.ch = 0;
//					tmp.cmd = 0x7B;
//					tmp.addr = 0;
//					tmp.dat = 0x08;
//					write_cmfbuf(&tmp);																
//				}
				if(ReturnData < 25 || ReturnData > 50)BEEP_ON();
					
				if(ReturnData > 25 && ReturnData < 50)
				{					
					tmp.ch = 0;
					tmp.cmd = 0x7D;
					tmp.addr = 0;															
					tmp.dat = 0x01;
					write_cmfbuf(&tmp);
				}
				disp_ui_v(ReturnData/100, ReturnData/10%10, ReturnData%10);	
		}				
						
			if(UIoperate.ContinuesFlag && ContinuesWrFlag)
			{
				ContinuesWrFlag = 0;
				ReturnData = 0;												
			}
		
			
}
